//
//  PushTopicTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/2/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "PushTopicTableViewCell.h"

@interface PushTopicTableViewCell ()<UITextFieldDelegate>

@property (nonatomic, weak)UIView * backView;


@end

@implementation PushTopicTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor  =ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIButton * iconButton = [UIButton new];
    
    [iconButton setImage:[UIImage imageNamed:@"话题添加图片"] forState:UIControlStateNormal];
    [iconButton addTarget:self action:@selector(clickImageButton) forControlEvents:UIControlEventTouchUpInside];
    self.iconButton = iconButton;
    
    [self.contentView addSubview:self.iconButton];
    
    
    UITextField * titleTextField = [[UITextField alloc] init];
    
    titleTextField.borderStyle = UITextBorderStyleNone;
//    titleTextField.backgroundColor = [UIColor clearColor];
//    titleTextField.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    titleTextField.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x909090);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    titleTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"#添加今日话题#(1~15个字)" attributes:attrs];
    titleTextField.textColor = ZRBlackColor;
    //    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //    //设置显示模式为永远显示(默认不显示)
    //    textFiled.leftViewMode = UITextFieldViewModeAlways;
    titleTextField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    titleTextField.delegate = self;

    self.titleTextField = titleTextField;
    
    [self.contentView addSubview:self.titleTextField];
}



- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(70 * SCREENH6);
    }];
    
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.height.width.mas_equalTo(50 * SCREENH6);
    }];
    
    [self.titleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconButton.mas_trailing).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
}

- (void)clickImageButton{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickImageViewButton)])
    {
        
        [self.delegate clickImageViewButton];
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.titleTextField.text.length >= 15) {
            self.titleTextField.text = [textField.text substringToIndex:15];
            return NO;
        }
    return YES;
}

@end
